package cn.service.impl;

import cn.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.Brand;
import entity.Category;
import entity.Result;
import mapper.CategoryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    CategoryMapper categoryMapper;

    /**
     * 查询level0级别的category数据
     * @return
     */
    @Override
    public Result queryCategories(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Category> page = (Page<Category>)categoryMapper.selectByLevel0(0);
        return Result.success((int)page.getTotal(),page.getResult());
    }

    @Override
    public void updateNav_status(Long category_id,Integer nav_status) {
        Category category = new Category(category_id,nav_status);
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public void updateShow_status(Long category_id, Integer show_status) {
        categoryMapper.updateShow_status(category_id,show_status);
    }

    @Override
    public Result deleteByPrimaryKey(Long category_id) {
        if (categoryMapper.deleteByPrimaryKey(category_id) > 0) {
            return Result.success(200,"删除成功!");
        } else {
            return Result.fail(500,"删除失败!");
        }
    }

    @Override
    public Result addCategoryLevel0(Category category) {
        if (categoryMapper.insertSelective(category) > 0) {
            return Result.success(200,"添加成功!");
        } else {
            return Result.fail(200,"添加失败!");
        }
    }

    @Override
    public Result queryByCategory_Id(Long category_id) {
        return Result.success(categoryMapper.selectByPrimaryKey(category_id));
    }

    @Override
    public Result categoryEditLevel0(Category category) {
        if (categoryMapper.updateByPrimaryKeySelective(category) > 0) {
            return Result.success(200,"编辑成功!");
        } else {
            return Result.fail(500,"编辑失败!");
        }
    }

    @Override
    public Result queryCategoriesLevel1(Long category_id, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Category> categories = categoryMapper.queryByParent_id(category_id);
        Page<Category> page = (Page<Category>)categories;
        return Result.success((int)page.getTotal(),categories);
    }


}
